<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="档案名称" prop="archiveName">
        <el-input
          v-model="queryParams.archiveName"
          placeholder="请输入档案名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="120px" label="借阅申请人名称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入借阅申请人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label-width="130px"
        label="借阅申请提交时间"
        prop="borrowApplyTime"
      >
        <el-date-picker
          clearable
          v-model="queryParams.borrowApplyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择借阅申请提交的时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审批状态" prop="shenpiState">
        <el-select
          v-model="queryParams.shenpiState"
          placeholder="请选择审批状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.apply_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="借阅申请人所在企业的企业级档案主管的id，user_id" prop="enterpriseAdminId">
        <el-input
        v-model="queryParams.enterpriseAdminId"
        placeholder="请输入借阅申请人所在企业的企业级档案主管的id，user_id"
        clearable
        @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借阅申请人所在企业的企业级档案主管的审批意见" prop="enterpriseAdminOpinion">
        <el-input
          v-model="queryParams.enterpriseAdminOpinion"
          placeholder="请输入借阅申请人所在企业的企业级档案主管的审批意见"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借阅申请人所在企业的企业级档案主管的备注说明" prop="enterpriseAdminNote">
        <el-input
          v-model="queryParams.enterpriseAdminNote"
          placeholder="请输入借阅申请人所在企业的企业级档案主管的备注说明"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-if="menu === 'arch'"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:borrow:add']"
          >借阅档案</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:borrow:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:borrow:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="borrowList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="借阅申请编号" align="center" prop="id" />
      <el-table-column label="档案编号" align="center" prop="archiveId" />
      <el-table-column label="档案名称" align="center" prop="archiveName" />
      <el-table-column label="借阅申请人编号" align="center" prop="userId" />
      <el-table-column label="借阅申请人名称" align="center" prop="username" />
      <el-table-column
        label="借阅开始时间"
        align="center"
        prop="borrowStartTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.borrowStartTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="借阅结束时间"
        align="center"
        prop="borrowEndTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.borrowEndTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="借阅原因说明"
        align="center"
        prop="borrowExplain"
      />
      <el-table-column
        label="借阅申请提交时间"
        align="center"
        prop="borrowApplyTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.borrowApplyTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" align="center" prop="shenpiState">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.apply_status"
            :value="scope.row.shenpiState"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="企业级档案管理员的名称"
        align="center"
        prop="enterpriseAdminName"
      />
      <el-table-column
        label="企业级档案管理员的编号"
        align="center"
        prop="enterpriseAdminId"
      />
      <el-table-column
        label="企业级档案管理员的审批意见"
        align="center"
        prop="enterpriseAdminOpinion"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.approval_opinion"
            :value="scope.row.enterpriseAdminOpinion"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="企业级档案管理员的备注说明"
        align="center"
        prop="enterpriseAdminNote"
      />
      <el-table-column
        label="政府级档案管理员的名称"
        align="center"
        prop="governmentAdminName"
      />
      <el-table-column
        label="政府级档案管理员的编号"
        align="center"
        prop="governmentAdminId"
      />
      <el-table-column
        label="政府级档案管理员的审批意见"
        align="center"
        prop="governmentAdminOpinion"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.approval_opinion"
            :value="scope.row.governmentAdminOpinion"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="政府级档案管理员的备注说明"
        align="center"
        prop="governmentAdminNote"
      />
      <el-table-column
        v-if="menu !== 'arch'"
        fixed="right"
        label="操作"
        width="200px"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="menu === 'en'"
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            @click="handleUpdateEnterprise(scope.row)"
            v-hasPermi="['system:borrow:edit']"
            >企业审批</el-button
          >

          <el-button
            v-if="menu === 'gv'"
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            @click="handleUpdateGovernment(scope.row)"
            v-hasPermi="['system:borrow:edit']"
            >政府审批</el-button
          >

          <el-button
            v-if="scope.row.governmentAdminOpinion == 1"
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="viewFile(scope.row)"
            v-hasPermi="['system:borrow:edit']"
            >查看档案</el-button
          >

          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:borrow:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框 -->
    <!-- 档案借阅申请对话框 -->
    <el-dialog
      v-if="title === '档案借阅申请'"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <!-- <el-form-item label="档案编号" prop="archiveId">
          <el-input v-model="form.archiveId" placeholder="请输入档案id" />
        </el-form-item>
        <el-form-item label="档案名称" prop="archiveName">
          <el-input v-model="form.archiveName" placeholder="请输入档案名称" />
        </el-form-item> -->
        <el-form-item label="档案名称" prop="archiveId">
          <el-select
            v-model="form.archiveId"
            filterable
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in arOptions"
              :key="item.id"
              :label="item.archiveName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="借阅申请人编号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入借阅申请人id，user_id" />
        </el-form-item>
        <el-form-item label="借阅申请人名称" prop="username">
          <el-input v-model="form.username" placeholder="请输入借阅申请人名称" />
        </el-form-item> -->
        <!-- <el-form-item label="借阅开始时间" prop="borrowStartTime">
          <el-date-picker clearable
            v-model="form.borrowStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择借阅开始时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="借阅结束时间" prop="borrowEndTime">
          <el-date-picker
            clearable
            v-model="form.borrowEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择借阅结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="借阅原因说明" prop="borrowExplain">
          <el-input
            v-model="form.borrowExplain"
            placeholder="请输入借阅原因说明"
          />
        </el-form-item>
        <!-- <el-form-item label="提交借阅申请的时间，就是插入数据的时间" prop="borrowApplyTime">
          <el-date-picker clearable
            v-model="form.borrowApplyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择提交借阅申请的时间，就是插入数据的时间">
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="审批状态，只有待审批、正在审批、审批结束" prop="shenpiState">
          <el-radio-group v-model="form.shenpiState">
            <el-radio
              v-for="dict in dict.type.apply_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 档案借阅企业审批对话框 -->
    <el-dialog
      v-if="title === '档案借阅企业审批'"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item
          label="企业级档案管理员审批意见"
          prop="enterpriseAdminOpinion"
        >
          <el-select
            v-model="form.enterpriseAdminOpinion"
            placeholder="请选择审批意见"
            clearable
          >
            <el-option
              v-for="dict in dict.type.approval_opinion"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="企业级档案管理员备注说明"
          prop="enterpriseAdminNote"
        >
          <el-input
            v-model="form.enterpriseAdminNote"
            placeholder="请输入备注说明"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 档案借阅政府审批对话框 -->
    <el-dialog
      v-if="title === '档案借阅政府审批'"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item
          label="政府级档案管理员审批意见"
          prop="governmentAdminOpinion"
        >
          <el-select
            v-model="form.governmentAdminOpinion"
            placeholder="请选择审批意见"
            clearable
          >
            <el-option
              v-for="dict in dict.type.approval_opinion"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="政府级档案管理员备注说明"
          prop="governmentAdminNote"
        >
          <el-input
            v-model="form.governmentAdminNote"
            placeholder="请输入备注说明"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 档案查看对话框 -->
    <el-dialog
      v-if="title === '档案查看'"
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <el-tag type="info" v-if="file.filePath == null || file.filePath == ''"
        >无文件</el-tag
      >
      <div v-if="file.filePath != null && file.filePath !== ''">
        <span
          :key="path"
          class="el-upload-list__item ele-upload-list__item-content"
          v-for="(path, index) in getFilePath(file.filePath)"
        >
          <el-link :href="path" :underline="false" target="_blank">
            <span class="el-icon-document">{{ getFileName(path) }}</span>
          </el-link>
        </span>
      </div>
      <el-tag type="info" v-if="file.imagePath == null || file.imagePath === ''"
        >无图片</el-tag
      >
      <image-preview
        v-if="file.imagePath != null && file.imagePath !== ''"
        :src="file.imagePath"
        :width="50"
        :height="50"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  listBorrow,
  getBorrow,
  delBorrow,
  addBorrow,
  updateBorrow,
} from "@/api/system/borrow";
import {
  listDArchive,
  getDArchive,
  delDArchive,
  addDArchive,
  updateDArchive,
} from "@/api/system/DArchive";
import { getInfo } from "@/api/login";

export default {
  name: "Borrow",
  dicts: ["apply_status", "approval_opinion"],
  data() {
    return {
      // 项目根路径
      baseUrl: process.env.VUE_APP_BASE_API,
      file: null,
      menu: null,
      user: null,
      arOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 档案借阅表格数据
      borrowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        archiveId: null,
        archiveName: null,
        userId: null,
        username: null,
        borrowStartTime: null,
        borrowEndTime: null,
        borrowExplain: null,
        borrowApplyTime: null,
        shenpiState: null,
        enterpriseAdminName: null,
        enterpriseAdminId: null,
        enterpriseAdminOpinion: null,
        enterpriseAdminNote: null,
        governmentAdminId: null,
        governmentAdminName: null,
        governmentAdminStatus: null,
        governmentAdminNote: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        archiveId: [
          { required: true, message: "档案编号不能为空", trigger: "blur" },
        ],
        archiveName: [
          { required: true, message: "档案名称不能为空", trigger: "blur" },
        ],
        userId: [
          {
            required: true,
            message: "借阅申请人编号不能为空",
            trigger: "blur",
          },
        ],
        username: [
          {
            required: true,
            message: "借阅申请人名称不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getArchList();
    getInfo().then((res) => {
      this.user = res.user;
    });
    // 获取菜单信息
    this.menu = this.$route.query.menu;
  },
  methods: {
    /** 获取档案下拉菜单 */
    getArchList() {
      listDArchive({ archStatus: 0, pageNum: 1, pageSize: 100 }).then(
        (response) => {
          this.arOptions = response.rows;
        }
      );
    },
    /** 查询档案借阅列表 */
    getList() {
      this.loading = true;
      listBorrow(this.queryParams).then((response) => {
        this.borrowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        archiveId: null,
        archiveName: null,
        userId: null,
        username: null,
        borrowStartTime: null,
        borrowEndTime: null,
        borrowExplain: null,
        borrowApplyTime: null,
        shenpiState: null,
        enterpriseAdminName: null,
        enterpriseAdminId: null,
        enterpriseAdminOpinion: null,
        enterpriseAdminNote: null,
        governmentAdminId: null,
        governmentAdminName: null,
        governmentAdminStatus: null,
        governmentAdminNote: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "档案借阅申请";
    },
    /** 企业审批按钮操作 */
    handleUpdateEnterprise(row) {
      if (row.shenpiState == 0 && row.enterpriseAdminOpinion == 0) {
        this.reset();
        this.open = true;
        const id = row.id || this.ids;
        getBorrow(id).then((response) => {
          this.form = response.data;
          this.form.enterpriseAdminId = this.user.userId;
          this.form.enterpriseAdminName = this.user.userName;
          this.title = "档案借阅企业审批";
        });
      } else {
        this.$modal.msgError("非待审批状态！");
      }
    },
    handleUpdateGovernment(row) {
      if (row.enterpriseAdminOpinion == 1 && row.governmentAdminOpinion == 0) {
        this.reset();
        this.open = true;
        const id = row.id || this.ids;
        getBorrow(id).then((response) => {
          this.form = response.data;
          this.form.governmentAdminId = this.user.userId;
          this.form.governmentAdminName = this.user.userName;
          this.title = "档案借阅政府审批";
        });
      } else {
        this.$modal.msgError("非待审批状态！");
      }
    },
    viewFile(row) {
      this.file = row;
      this.title = "档案查看";
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.shenpiState = this.form.governmentAdminOpinion;
            updateBorrow(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBorrow(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除档案借阅编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBorrow(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/borrow/export",
        {
          ...this.queryParams,
        },
        `borrow_${new Date().getTime()}.xlsx`
      );
    },
    // 文件路径处理
    getFilePath(filePaths) {
      var arrayPath = filePaths.split(",");
      for (let i in arrayPath) {
        arrayPath[i] = this.baseUrl + arrayPath[i];
      }
      return arrayPath;
    },

    // 获取文件名称
    getFileName(name) {
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1).toLowerCase();
      } else {
        return "";
      }
    },
  },
};
</script>
